package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.exception.LogistException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySaechQuery;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.anno.UserParm;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/strategy")
@Controller
public class StrategyController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService commentService;
    @Reference
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategySearchService strategySearchService;

       @RequestMapping("/detail")
    public Object detail(Long id, Model model,@UserParm UserInfo userInfo){
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);
        model.addAttribute("detail",detail);

        //浏览数
        strategyStatisVORedisService.viewnumIncrease(id,1);
        model.addAttribute("vo",strategyStatisVORedisService.getStrategyStatisVo(id));
        //加分
        strategyStatisVORedisService.addHot(id,1);
        //收藏回显
        if (userInfo!=null){
            Boolean isFavor = strategyStatisVORedisService.isFavor(detail.getId(),userInfo.getId());
            System.out.println(isFavor);
            model.addAttribute("isFavor",isFavor);
        }
        return "/strategy/detail";
    }

    @RequestMapping("/list")
    public Object list(@ModelAttribute("qo") StrategyDetailQuery qo , Model model){
         if (qo==null){

         }
        //toast
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest",dest);
        model.addAttribute("toasts",toasts);
        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        //tags
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags",tags);
        return "/strategy/list";
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParm UserInfo userInfo){
        if (userInfo==null){
            throw  new LogistException("请登录");
        }
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        commentService.save(comment);
        //评论数加1
        strategyStatisVORedisService.replyNumIncrease(comment.getDetailId(),1);
        int replynum = strategyStatisVORedisService.getStrategyStatisVo(comment.getDetailId()).getReplynum();
        //加分
        strategyStatisVORedisService.addHot(comment.getDetailId(),1);
        return AjaxResult.SUCCESS.addData(replynum);
    }

    @RequestMapping("")
    public String strategy(Model model){
        //abroadCds 海外攻略推荐排行
        List<StrategyStatisVO> vos = strategyStatisVORedisService.listCommendVO();
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        for (StrategyStatisVO vo : vos) {
            if (vo.isIsabroad()){
                //国外
                if (abroadCds.size()<10){
                    abroadCds.add(vo);
                }
            }else {
                //国内
                if (unabroadCds.size()<10){
                    unabroadCds.add(vo);
                }
            }
        }
        model.addAttribute("abroadCds",abroadCds);
        //unabroadCds 国内攻略推荐排行
        model.addAttribute("unabroadCds",unabroadCds);
        //hotCds 热门攻略推荐排行前10
        List<StrategyStatisVO> hotCds = strategyStatisVORedisService.getHotVOTop10();
        model.addAttribute("hotCds",hotCds);
        //themeCds 主题推荐
        List<Map<String,Object>> list = strategySearchService.queryThemeTop10();
        model.addAttribute("themeCds",list);
        //commends 查询前五
        List<StrategyCommend> commends = strategyCommendService.getHot5();
        model.addAttribute("commends",commends);
        //chinas
        List<StatisVO> chinas = strategySearchService.queryConditionProvincesGroup();
        model.addAttribute("chinas",chinas);
        //abroads
        List<StatisVO> abroads = strategySearchService.queryConditionAbroadsGroup();
        model.addAttribute("abroads",abroads);
        //themes
        List<StatisVO>themes = strategySearchService.queryConditionThemeGroup();
        model.addAttribute("themes",themes);
        return "/strategy/index";
    }

    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo")StrategyCommentQuery qo){
        model.addAttribute("page",commentService.query(qo));
        return "/strategy/commentTpl";
    }

    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,Long fromid){
        commentService.commentThumbUp(toid,fromid);
        return AjaxResult.SUCCESS;
    }

    //攻略收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParm UserInfo userInfo){
        if (userInfo ==null){
            AjaxResult result = new AjaxResult(false,"请登录");
            result.setCode(102);
            return  result;
        }
        //boolean ret = strategyStatisVORedisService.favor(sid,userInfo.getId());

        boolean ret = strategyStatisVORedisService.favorByUser(userInfo.getId(), sid);
        //因为收藏有添加收藏和取消收藏,因此需要判断一下是true还是false
        if (ret){
            strategyStatisVORedisService.addCommendScore(sid,1);
        }else {
            strategyStatisVORedisService.addCommendScore(sid,-1);
        }
        return new AjaxResult(ret,null).addData(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParm UserInfo userInfo){
        if (userInfo ==null){
            AjaxResult result = new AjaxResult(false,"请登录");
             result.setCode(102);
             return result;
        }
        boolean ret = strategyStatisVORedisService.strategyThumbup(sid,userInfo.getId());
        //点赞加分
        if (ret){
            strategyStatisVORedisService.addCommendScore(sid,1);
        }else {
            strategyStatisVORedisService.addCommendScore(sid,-1);
        }
        return new AjaxResult(ret,null).addData(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }

    @RequestMapping("searchPage")
    public String searchPage(Model model, @ModelAttribute("qo")StrategySaechQuery qo){
        Page page =  strategySearchService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }

}
